<?php

class Ajax {

    public static function action_index() {
        
    }

    public static function updateDailyLoginBonus($userid) {
        if ($userid) { //get user info by ID
            $data = Frontend::findUserById($userid);
            foreach ($data as $_data) {
                $userinfo = $_data;
            }
            if (isset($userinfo)) {
                //remove _id
                unset($userinfo['_id']);
                $logindate = $userinfo['logindate'];
                //check user is add coin for bonus login
                $datenowTS = strtotime(date('Y-m-d') . ' 00:00:00');
                $bonus = false;
                if ($datenowTS > intval($logindate)) { //new date login
                    //get user gold, fix add gold value
                    $userinfo['gold'] = intval($userinfo['gold']) + 50;
                    Frontend::onUpdate('users', $userinfo, $userid);
                    $bonus = true;
                }
                //update login time
                if ($bonus) {
                    $userinfo['isBonus'] = 1;
                }
                $userinfo['logindate'] = strtotime(date('Y-m-d H:i:s'));
                Frontend::onUpdate('users', $userinfo, $userid);
            }
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_login($param) {
        $data = Frontend::findUserLogin($param["email"], $param["pass"]);
        if (iterator_to_array($data) == null) {
            echo "Email hoặc mật khẩu không đúng.";
        } else {
            foreach ($data as $_data) {
                Session::set_session("fullname", $_data["fullname"]);
                Session::set_session("email", $_data["email"]);
                $userid = $_data["_id"];
                Session::set_session("user_id", $userid);
            }
            //
            if (isset($userid)) {
                Ajax::updateDailyLoginBonus($userid);
            }
            echo "1";
        }
    }
    /**
     * 
     * @param type $param
     * 
     */
    public static function action_login_user($param){
        $data = Frontend::findUserLogin($param["email"], $param["pass"]);
        if (iterator_to_array($data) == null) {
            echo "Email hoặc mật khẩu không đúng.";
        } else {
            foreach ($data as $_data) {
                Session::set_session("fullname", $_data["fullname"]);
                Session::set_session("email", $_data["email"]);
                $userid = $_data["_id"];
                Session::set_session("user_id", $userid);
            }
            //
            if (isset($userid)) {
                Ajax::updateDailyLoginBonus($userid);
            }
            echo "1";
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_change_pass($param) {
        $data = Frontend::findUserByPass($param["curpass"], (int) $param["id"]);
        if (iterator_to_array($data) == null) {
            echo "Mật khẩu cũ không đúng.";
        } else {
            try {
                foreach ($data as $_data) {
                    $_data = $_data + array("newpass" => $param["pass"]);
                    Sys::updateUserPass($_data);
                    echo "Thay đổi thành công.";
                }
            } catch (Exception $exc) {
                echo "Thay đổi mật khẩu lỗi.";
                die();
            }
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_change_user_info($param) {
        $data = Frontend::findUserById($param["id"]);
        if (iterator_to_array($data)) {
            foreach ($data as $_data) {
                $param = $param + array("email" => $_data["email"]);
                $param = $param + array("password" => $_data["password"]);
                $param = $param + array("joindate" => $_data["joindate"]);
                $param = $param + array("gold" => $_data["gold"]);
                $param = $param + array("silver" => $_data["silver"]);
                $param = $param + array("status" => $_data["status"]);
                try {
                    Sys::updateUserInfo($param);
                    echo "Thay đổi thông tin thành công.";
                } catch (Exception $exc) {
                    echo "Thay đổi thông tin lỗi";
                }
            }
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_admin_login($param) {
        $data = Frontend::findAdminLogin($param["username"], $param["password"]);
        if (iterator_to_array($data) == null) {
            echo "0";
        } else {
            foreach ($data as $_data) {
                if ($param["rememberme"] == "on") {
                    Session::set_cookie("username", $_data["username"], time() * 3600);
                    Session::set_cookie("id", $_data["_id"], time() * 3600);
                    Session::set_session("username", $_data["username"]);
                    Session::set_session("id", $_data["_id"]);
                } else {
                    Session::set_session("username", $_data["username"]);
                    Session::set_session("id", $_data["_id"]);
                }
            }
            echo "1";
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_add_odd($param) {
        $base_decode = base64_decode($param["values"]);
        $json_decode = json_decode($base_decode);
        $id = Frontend::findOddById($json_decode->id);
        $time = $json_decode->time;
        $flag = false;
        $today = date('Y-m-d h:i');
        $_today = strtotime($today);
        if ($time > $_today) {
            if (iterator_to_array($id)) {
                if (isset($_SESSION["cart"]["bongda"])) {
                    foreach ($_SESSION["cart"]["bongda"] as $_cart) {
                        $session_decode = base64_decode($_cart);
                        $session_decode = json_decode($session_decode);
                        if (($json_decode->id == $session_decode->odd_id) && ($json_decode->value == $session_decode->odd_value)) {
                            $flag = true;
                        }
                    }
                }
                if (!$flag) {
                    foreach ($id as $_id) {
                        $value = Frontend::findOddByValue($json_decode->value);
                        if (iterator_to_array($value)) {
                            $arr = array(
                                "odd_id" => "$json_decode->id",
                                "odd_value" => "$json_decode->value",
                                "match_id" => $_id['matchID'],
                                "time" => $json_decode->time,
                            );
                            $json_encode = json_encode($arr);
                            $base_encode = base64_encode($json_encode);
                            $_SESSION["cart"]["bongda"][] = $base_encode;
                            echo count($_SESSION["cart"]["soxo"]) + count($_SESSION["cart"]["bongda"]);
                        } else {
                            echo count($_SESSION["cart"]["soxo"]) + count($_SESSION["cart"]["bongda"]);
                        }
                    }
                } else {
                    echo "0";
                }
            }
        } else {
            echo "-1";
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_add_odd_sx($param) {
        $value = ($param["values"] >= 0 && $param["values"] < 10) ? "0" . $param["values"] : $param["values"];
        $location = $param["location"];
        $type = $param["type"];
        $flag = false;
        if (isset($_SESSION["cart"]["soxo"])) {
            foreach ($_SESSION["cart"]["soxo"] as $key => $_cart) {
                $session_decode = base64_decode($_cart);
                $session_decode = json_decode($session_decode);
                if ($value == $session_decode->value && $location == $session_decode->location && $type == $session_decode->type) {
                    $flag = true;
                }
            }
        }
        if (!$flag) {
            $today = date('Y-m-d');
            $time = strtotime(date("Y-m-d", strtotime($today)) . " +1 week");
            $arr = array(
                "value" => "$value",
                "location" => "$location",
                "type" => "$type",
                "time" => $time,
            );
            $json_encode = json_encode($arr);
            $base_encode = base64_encode($json_encode);
            $_SESSION["cart"]["soxo"][] = $base_encode;
            echo count($_SESSION["cart"]["soxo"]) + count($_SESSION["cart"]["bongda"]);
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_update_datcuoc($param) {
        $base_decode = base64_decode($param["values"]);
        $json_decode = json_decode($base_decode, true);
        if ($param["type"] == "bongda") {
            if (!$json_decode["money"]) {
                $total_money = $json_decode["odd_value"] * $param["money"];
                $json_decode = $json_decode + array("money" => $param["money"]);
                $json_decode = $json_decode + array("total_money" => $total_money);
                $json_decode = $json_decode + array("index_key" => $param["index_key"]);
            } else {
                $total_money = $json_decode["odd_value"] * $param["money"];
                $json_decode["money"] = $param["money"];
                $json_decode["total_money"] = $total_money;
                $json_decode["index_key"] = $param["index_key"];
            }
            $json_encode = json_encode($json_decode);
            $base_encode = base64_encode($json_encode);
            $_SESSION["cart"]["bongda"][$param["key"]] = $base_encode;
            echo $total_money;
        }
        if ($param["type"] == "soxo") {
            if (!$json_decode["money"]) {
                if ($json_decode["type"] == 1) {
                    $total_money = $param["money"] * 70;
                } else {
                    $total_money = $param["money"] * 3;
                }
                $json_decode = $json_decode + array("money" => $param["money"]);
                $json_decode = $json_decode + array("total_money" => $total_money);
            } else {
                if ($json_decode["type"] == 1) {
                    $total_money = $param["money"] * 70;
                } else {
                    $total_money = $param["money"] * 3;
                }
                $json_decode["money"] = $param["money"];
                $json_decode["total_money"] = $total_money;
            }
            $json_encode = json_encode($json_decode);
            $base_encode = base64_encode($json_encode);
            $_SESSION["cart"]["soxo"][$param["key"]] = $base_encode;
            echo $total_money;
        }
    }
    /**
     * 
     * @param type $param
     * 
     */
    public static function action_delete_datcuoc($param) {
        unset($_SESSION["cart"][$param["type"]][$param["key"]]);
        require_once 'app/views/frontend/_danhsachdudoan.php';
    }
    /**
     * 
     * @param type $param
     * 
     */
    public static function action_change_danhsach($param){
        require_once 'app/views/frontend/danhsachdudoan_change.php';
    }
    /**
     * 
     * @param type $param
     * 
     */
    public static function action_change_xoso($param){
        $today = date('Y-m-d');
        $today_ts = strtotime($today);
        $yesterday = strtotime(date("Y-m-d", strtotime($today)) . " -1 day");
        $odd_sx = Frontend::findOddSxMB($today_ts, $yesterday, (int)$param["key"]);
        if(iterator_to_array($odd_sx) != null){
            require_once 'app/views/frontend/_dudoanxoso.php';
        }else{
            echo "Chưa có kêt quả.";
        }
    }
    /**
     * 
     * @param type $param
     *  
     */
    public static function action_trade($param){
        $prize = Frontend::findPrizeById((int)$param["prize_id"]);
        foreach ($prize as $_prize)  {
            try {
                Sys::updatePrize($_prize);
                Sys::addUserPrize($param["user_id"], $param["prize_id"]);
                echo "Bạn đã đổi giải thưởng thành công chúng tối sẽ liên lạc với bạn sớm nhất.";
            } catch (Exception $exc) {
                echo "0";
            }
        }
        
    }

}
?>

